package com.atguigu.controller;

import com.atguigu.pojo.Employee;
import com.atguigu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    /**
     * 获取所有员工信息
     * @return
     */
    @GetMapping("/emp")
    public List<Employee> findAllEmp() {
        return service.findAllEmp();
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/emp/{id}")
    public Employee getEmpById(@PathVariable Integer id) {
        return service.getEmpById(id);
    }

    /**
     * 添加员工
     * @param emp
     * @return
     */
    @PostMapping("/emp")
    public String saveEmp(@RequestBody Employee emp) {
        service.saveEmp(emp);
        return "success";
    }

    /**
     * 根据 id 删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/emp/{id}")
    public String deleteEmp(@PathVariable Integer id){
        service.deleteEmp(id);
        return "success";
    }

    /**
     * 修改员工信息
     * @param emp
     * @return
     */
    @PutMapping("/emp")
    public String updateEmp(@RequestBody Employee emp){
        service.updateEmp(emp);
        return "success";
    }
}
